package com.shop.service.impl;

import com.shop.dao.ContractProductDao;
import com.shop.domain.ContractProduct;
import com.shop.domain.Page;
import com.shop.service.ContractProductService;
import com.shop.util.IDUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * ContractProductServiceImpl
 * Created by heqianqian on 2017/7/12.
 */
@Service
public class ContractProductServiceImpl implements ContractProductService<ContractProduct> {

    @Resource
    private ContractProductDao contractProductDao;

    @Override
    @SuppressWarnings("unchecked")
    public List<ContractProduct> findPage(Page page) {
        return contractProductDao.findPage(page);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<ContractProduct> find(Map paraMap) {
        return contractProductDao.find(paraMap);
    }

    @Override
    public ContractProduct get(Serializable id) {
        return (ContractProduct) contractProductDao.get(id);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void insert(ContractProduct contractProduct) {
        //设置主键
        contractProduct.setContractProductId(IDUtils.getId());
        //计算总金额
        if (!StringUtils.isEmpty(contractProduct.getCnumber())
                && !StringUtils.isEmpty(contractProduct.getPrice())) {
            contractProduct.setAmount(contractProduct.getCnumber() * contractProduct.getPrice());
        }
        contractProductDao.insert(contractProduct);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void update(ContractProduct contractProduct) {
        //计算总金额
        if (!StringUtils.isEmpty(contractProduct.getCnumber())
                && !StringUtils.isEmpty(contractProduct.getPrice())) {
            contractProduct.setAmount(contractProduct.getCnumber() * contractProduct.getPrice());
        }
        contractProductDao.update(contractProduct);
    }

    @Override
    public void deleteById(Serializable id) {
        contractProductDao.deleteById(id);
    }

    @Override
    public void delete(Serializable[] ids) {
        contractProductDao.delete(ids);
    }
}
